package com.fingard.dsp.bank.directbank.mtcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @ClassName MTCB01Balance
 * @Author Zhengzy
 * @Date 2019/12/19 9:31
 * @Version 1.0
 **/
public class MTCB01Balance extends MTCB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        // 余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            WriteBankLogLn("进入余额查询处理类");
            while (p_req7506.readRecord()) {
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                MTCB01Header bankHeader = new MTCB01Header();
                bankHeader.trCode = "200108";
                bankHeader.reqNo = p_req7506.reqHeader.transSeqID;
                bankHeader.fileFlag = "0";
                writeCommonHeader(xmlTextWriter, bankHeader);

                // 组装报文体
                XElement tmpXElement = new XElement("body", "");
                tmpXElement.addChild(new XElement("acno", p_req7506.readedDetail.actNum));// 账号
                tmpXElement.addChild(new XElement("cur_code", convertCurToBank(p_req7506.readedDetail.actCur)));// 币种
                xmlTextWriter.writeXElement(tmpXElement);
                xmlTextWriter.writeEndDocument();

                // 发送给银行
                String[] tmpRetBkStr = sendToBank(xmlTextWriter.toXmlString());
                /*String[] tmpRetBkStr = {"", "<ap>\n" +
                        "\t<head>\n" +
                        "\t\t<tr_code>交易码</tr_code>\n" +
                        "        <cms_corp_no>企业代码</cms_corp_no>\n" +
                        "        <user_no>操作员号</user_no>\n" +
                        "        <org_code>机构号</org_code>\n" +
                        "        <serial_no>交易序号</serial_no>\n" +
                        "        <req_no>发起方序号</req_no>\n" +
                        "        <tr_acdt>交易日期</tr_acdt>\n" +
                        "        <tr_time>交易时间</tr_time>\n" +
                        "        <succ_flag>1</succ_flag>\n" +
                        "        <ret_info>返回信息</ret_info>\n" +
                        "        <ret_code>0000</ret_code>\n" +
                        "        <ext_info>返回扩展信息</ext_info>\n" +
                        "        <file_flag>文件标识</file_flag>\n" +
                        "        <reserved>保留位</reserved>\n" +
                        "\t</head>\n" +
                        "\t<body>\n" +
                        "\t   <acno>你的账号</acno>\n" +
                        "\t   <cur_code>1</cur_code>\n" +
                        "\t   <acname>民泰浙商银行</acname>\n" +
                        "\t   <balance>1800000</balance>\n" +
                        "\t   <use_balance>1700000</use_balance>\n" +
                        "\t   <freeze_bal>100000</freeze_bal>\n" +
                        "\t</body>\n" +
                        "</ap>"};*/
                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        // 返回码 0000表示成功，正常返回 ,9999，1102两个为结果未知，需要查证
                        String returnCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                        // 返回信息
                        String returnMsg = tmpBkReader.getFirstText("/ap/head/ret_info");
                        if ("0000".equals(returnCode)) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";// 执行状态码
                            tmpRetDetail.respInfo = returnMsg;
                            tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                            tmpRetDetail.actArea = p_req7506.readedDetail.actArea;

                            tmpRetDetail.currBal = tmpBkReader.getFirstText("/ap/body/balance");
                            tmpRetDetail.availBal = tmpBkReader.getFirstText("/ap/body/use_balance");
                            tmpRetDetail.frozenBal = tmpBkReader.getFirstText("/ap/body/freeze_bal");
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        } else if ("9999".equals(returnCode)) {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo("银企直联通讯机校验错误，或者通讯失败！");
                            break;
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(returnMsg);
                            break;
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                        break;
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRetBkStr[1]);
                    break;
                }
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            WriteBankLogLn("余额查询异常：" + e);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
